<?php

require_once 'DML.class.php';
require_once 'DBUsuario.class.php';
/*@author Gabriel Bronzatti Moro*/
Class DBCarona extends DML {

    private $table;
    private $conection;
    private $instrucao;
    private $dbUsuario;

    public function __construct($mysqli) {
        $this->table = "carona";
        $this->conection = $mysqli;
    }

    public function insert($colunas, $valores) {
        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("INSERT INTO " . $this->table . "(" . $colunas . ") VALUES(" . $valores . ");");
            return $resultSet;
        } else
            return false;
    }

//`hora`,`cidadeOrigem`,`cidadeDestino`,"
//  . "`usuario_codigo_usuario`,`status_carona`,`preco_carona`
    
    public function select($colunas, $valores) {
       if(isset($this->conection)){
          $resultSet= $this->instrucao= $this->conection->query("SELECT ".$colunas." FROM ".
          $this->table . " WHERE `usuario_codigo_usuario` !=".$valores[0]." and `cidadeOrigem`= '"
                  .$valores[1]."' and `cidadeDestino`= '".
         $valores[2]."';");

          $i=0;
          $array= null;
         
          $this->dbUsuario= new DBUsuario($this->conection);
          while ($row= $resultSet->fetch_object())
          {
              $array[$i]= $row->hora;
              $array[$i+1]= $row->cidadeOrigem;
              $array[$i+2]= $row->cidadeDestino;
              $nomeUser= $this->dbUsuario->recuperaNomeUser($row->usuario_codigo_usuario);
              $array[$i+3]= $nomeUser;
              $array[$i+4]= $row->status_carona;
              $array[$i+5]= $row->preco_carona;
              
              $i+=6;
          }
          return $array;
    }
    }

    public function recuperaCodigoUser($nome,$login) {

        if (isset($this->conection)) {
            $resultSet = $this->instrucao = $this->conection->query("SELECT  `codigo_usuario`" . " FROM " .
                    "usuario" . " WHERE `nome_usuario`= \"" . $nome . "\" and `email_usuario`=\"".$login."\";");

            while ($row = $resultSet->fetch_object()) {
                return $row->codigo_usuario;
            }
        }
    }
    
    public function selectMinhasCaronas($colunas, $valor){
       if(isset($this->conection)){
          $resultSet= $this->instrucao= $this->conection->query("SELECT ".$colunas." FROM ".
          $this->table . " WHERE `usuario_codigo_usuario` =\"".$valor."\";");

          $i=0;
          $array= null;
         
          while ($row= $resultSet->fetch_object())
          {
              $array[$i]= $row->hora;
              $array[$i+1]= $row->cidadeOrigem;
              $array[$i+2]= $row->cidadeDestino;
              $array[$i+3]= $row->status_carona;
              $array[$i+4]= $row->preco_carona;
              
              $i=$i+5;
          }
          return $array;
    }
    }

}